package com.xm.xmapp;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CustomLayout;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Charing_statingRequest;
import com.xm.xmapp.response.bean.BaseBeanResponse;
import com.xm.xmapp.response.bean.ResponseCharingStating;
import com.xm.xmapp.tools.StringTools;

public class PileUsingOrderDetailActivity extends BaseActivity implements OnRefreshListener {

	private CustomLayout orderMoney, chargeTime, chargeCount, orderTime, orderNumber, chargeAddress, chargeNumber,
			chargeMoney, serverMoney, chargeType, carrieroperator;

	private LinearLayout status_layout;
	private SwipeRefreshLayout id_swipe_ly;
	private String orderId;
	private ResponseCharingStating pileOrderDetailBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pile_usingorder_detail);
	}

	@Override
	public void initView() {
		orderMoney = (CustomLayout) findViewById(R.id.orderMoney);
		chargeTime = (CustomLayout) findViewById(R.id.chargeTime);
		chargeCount = (CustomLayout) findViewById(R.id.chargeCount);
		orderTime = (CustomLayout) findViewById(R.id.orderTime);
		orderNumber = (CustomLayout) findViewById(R.id.orderNumber);
		chargeAddress = (CustomLayout) findViewById(R.id.chargeAddress);
		chargeNumber = (CustomLayout) findViewById(R.id.chargeNumber);
		status_layout = (LinearLayout) findViewById(R.id.status_layout);
		id_swipe_ly = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
		chargeMoney = (CustomLayout) findViewById(R.id.chargeMoney);
		serverMoney = (CustomLayout) findViewById(R.id.serverMoney);
		chargeType = (CustomLayout) findViewById(R.id.chargeType);
		carrieroperator = (CustomLayout) findViewById(R.id.carrieroperator);
	}


	@Override
	public void initData() {
		setMyTitle("订单详情");
		orderId = getIntent().getStringExtra("orderId");
		status_layout.setVisibility(View.VISIBLE);
		getData(orderId);
		orderNumber.setText2(orderId);
		id_swipe_ly.setOnRefreshListener(this);

	}


	private void getData(String orderId) {
		Charing_statingRequest requestBean = new Charing_statingRequest();
		requestBean.orderNo = orderId;
		GetDataInterFace<String> listener = new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				id_swipe_ly.setRefreshing(false);

				BaseBeanResponse<String> baseBean = JSON.parseObject(response,
						new TypeReference<BaseBeanResponse<String>>() {
						});

				switch (baseBean.errCode) {
				case 0:
					pileOrderDetailBean = JSON.parseObject(baseBean.data, ResponseCharingStating.class);
					orderMoney.setText2(pileOrderDetailBean.TotalMoney + "元");

					chargeTime.setText2(StringTools.getTime(pileOrderDetailBean.CharginTime / 60));
					chargeCount.setText2(pileOrderDetailBean.Soc + "度");
					orderTime.setText2(StringTools
							.getTime(pileOrderDetailBean.StartTime * 1000, "yyyy-MM-dd  HH:mm:ss"));
					orderNumber.setText2(pileOrderDetailBean.orderNo);
//					chargeOrder.setVisibility(View.GONE);
					chargeAddress.setText2(pileOrderDetailBean.charging_pile_network_name);
					chargeNumber.setText2(pileOrderDetailBean.charging_pile_number);

					chargeMoney.setText2(pileOrderDetailBean.price + "元");
					serverMoney.setText2(pileOrderDetailBean.servicCost + "元");
					chargeType.setText2(pileOrderDetailBean.charging_fast_slow == 1 ? "快充" : "慢充");
					carrieroperator.setText2(pileOrderDetailBean.charging_operator_name);

					break;

				default:
					break;
				}

			}
		};

		MyAppcation.getMyAppcation().getPostData(this, requestBean, listener, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
				id_swipe_ly.setRefreshing(false);
			}
		});

	}

	@Override
	public void initEvent() {

	}

	@Override
	public void onRefresh() {
		if (status_layout.getVisibility() == View.VISIBLE) {
			promptDialog.show();
			getData(orderId);
		}
	}
}
